﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 所有消息的收发中心
/// </summary>
public class EventNodeCore : EventNode, IEventListener
{
    /// <summary>
    /// 单例化EventNodeCore
    /// </summary>
    private static EventNodeCore instance;
    public static EventNodeCore GetInstance()
    {
        return instance;
    }
    private void Awake()
    {
        instance = this;
    }
    private EventNodeCore() { }

    /// <summary>
    /// 优先级
    /// </summary>
    /// <returns></returns>
    public int EventPriority() { return 0; }

    /// <summary>
    /// 未完成 消息中心接收消息
    /// </summary>
    /// <param name="id"></param>
    /// <param name="param1"></param>
    /// <param name="param2"></param>
    /// <returns></returns>
    public bool HandleEvent(int id, object param1, object param2)
    {
        if (false) // 还啥都没有写
        {
            // 特殊情况
        }
        else 
        {
            SendMessage(id, param1, param2);
        }
        return false;
        /*
         * 默认就是转发消息，如果写入if则会截断转发，又要if又要转发那就再SendMessage一次
         */
    }

    /// <summary>
    /// 未完成 发送消息
    /// </summary>
    /// <param name="key">消息类型，来自EventDef</param>
    /// <param name="param1">参数1，可选</param>
    /// <param name="param2">参数2，可选</param>
    public void SendMessage(int key, object param1=null, object param2=null)
    {
        //TriggerEvent(key, param1, param2); // 消息触发格式
        TriggerEvent(key, param1, param2);
        //if(Mathf.Abs(key) > 9 && Mathf.Abs(key) < 20)

        //switch (key)
        //{
        //    case 0:
        //        TriggerEvent(EventDef.Click, null, null);
        //        break;
        //    case 1:
        //        TriggerEvent(EventDef.EnemyDeath, null, null);
        //        break;
        //    case 2:
        //        TriggerEvent(EventDef.EnemyHit, null, null);
        //        break;
        //}

    }
}



//异次元的消息发送

//public Dictionary<int, List<Action<object, object[]>>> _dics;

//public void Register(int msgType, Action<object, object[]> func)
//{
//    if (_dics == null)
//    {
//        _dics = new Dictionary<int, List<Action<object, object[]>>>();
//    }
//    if (!_dics.ContainsKey(msgType))
//    {
//        _dics[msgType] = new List<Action<object, object[]>>();
//    }
//    if (!_dics[msgType].Contains(func))
//    {
//        _dics[msgType].Add(func);
//    }
//}

//public void Remove(int msgType, Action<object, object[]> func)
//{
//    if (_dics == null)
//    {
//        return;
//    }
//    if (!_dics.ContainsKey(msgType))
//    {
//        return;
//    }
//    if (_dics[msgType].Contains(func))
//    {
//        _dics[msgType].Remove(func);
//    }
//}

//public void SendMasage(int msgType, object sender, object[] param)
//{
//    if (_dics == null || !_dics.ContainsKey(msgType))
//    {
//        Debug.LogError("发送了错误消息");
//        return;
//    }

//    foreach (var func in _dics[msgType])
//    {
//        if (func != null)
//        {
//            func(sender, param);
//        }
//    }
//}


//public void Init()
//{
//    Register(0, Receive);
//}

//public void SenderFunc()
//{
//    SendMasage(0, this, new object[] { 123, "fxxk you" });
//}

//public void Receive(object sender, object[] param)
//{
//    Assert.AreEqual(2, param.Length);

//    int int1 = (int)param[0];
//    string strs = param[1] as string;
//    Debug.Log("sender:" + sender);
//    Debug.Log("number:" + int1);
//    Debug.Log("strs:" + strs);
//}